package com.wxapp.common.constant.pojo.vm;


import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.wxapp.common.constant.enums.ResultEnum;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

/**
 * 返回数据
 */
public class ResponseVm<T> {

    public static final Logger log = LoggerFactory.getLogger("com.wxapp.common.constant.pojo.vm.ResultVm");

    public static <T> ResultVm<T> success(ResultEnum resultEnum, T data) {
        ResultVm<T> resultVm = new ResultVm<>();
        return resultVm.setCodeEnum(resultEnum).setMsg(resultEnum).setData(data);
    }


    public static <T> ResultVm<T> success(T data) {
        ResultVm<T> resultVm = new ResultVm<>();
        resultVm.setCodeEnum(ResultEnum.SUCCESS).setMsg(ResultEnum.SUCCESS).setData(data);
        return resultVm;
    }


    public static <T> ResultVm<T> success(String msg, T data) {
        ResultVm<T> resultVm = new ResultVm<>();
        resultVm.setCodeEnum(ResultEnum.SUCCESS).setMsg(msg).setData(data);
        return resultVm;
    }


    public static <T> ResultVm<T> success() {
        var resultVm = new ResultVm<T>();
        resultVm.setCodeEnum(ResultEnum.SUCCESS).setMsg(ResultEnum.SUCCESS);
        return resultVm;
    }


    public static <T> ResultVm<T> success(String msg) {
        var resultVo = new ResultVm<T>();
        resultVo.setCodeEnum(ResultEnum.SUCCESS).setMsg(msg);
        return resultVo;
    }


    public static <T> ResultVm<T> error(String msg) {
        var resultVo = new ResultVm<T>();
        resultVo.setCodeEnum(ResultEnum.BAD_REQUEST).setMsg(msg);
        return resultVo;
    }


    public static <T> ResultVm<T> error() {
        var resultVo = new ResultVm<T>();
        resultVo.setCodeEnum(ResultEnum.BAD_REQUEST).setMsg(ResultEnum.BAD_REQUEST);
        return resultVo;
    }


    public static <T> ResultVm<T> error(ResultEnum resultEnum, String msg) {
        var resultVo = new ResultVm<T>();
        resultVo.setCode(resultEnum.getValue()).setMsg(msg);
        return resultVo;
    }

    public static <T> ResultVm<T> error(ResultEnum resultEnum) {
        var resultVo = new ResultVm<T>();
        resultVo.setCode(resultEnum.getValue()).setMsg(resultEnum.getDesc());
        return resultVo;
    }

    public static <T> ResultVm<T> define(Integer code, String msg, T data) {
        var resultVo = new ResultVm<T>();
        resultVo.setCode(code).setMsg(msg).setData(data);
        return resultVo;
    }

    public static <T> ResultVm<T> define(ResultEnum resultEnum, String msg, T data) {
        ResultVm<T> resultVm = new ResultVm<>();
        resultVm.setCode(resultEnum.getValue()).setMsg(msg).setData(data);
        return resultVm;
    }

    public static <T> ResultVm<T> define(ResultEnum resultEnum, T data) {
        ResultVm<T> resultVm = new ResultVm<>();
        resultVm.setCode(resultEnum.getValue()).setMsg(resultEnum.getDesc()).setData(data);
        return resultVm;
    }

    public static ResultVm<String> exceptionHandler(long s, BlockException ex) {
        ex.printStackTrace();
        ResultVm<String> resultVm = new ResultVm<>();
        resultVm.setCode(ResultEnum.FREQUENT_REQUESTS.getValue()).setMsg(ResultEnum.FREQUENT_REQUESTS.getDesc()).setData(null);
        return resultVm;
    }

    protected void exportFile(HttpServletResponse response, byte[] files, String name) {
        if (response == null) {
            return;
        }
        response.setContentType("application/octet-stream;charset=UTF-8");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-cache,no-store");
        OutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            if (name != null && !"".equals(name)) {
                response.setHeader("Content-Disposition", "attachment;filename=" + new String(name.getBytes(), StandardCharsets.UTF_8));
            }
            outputStream.write(files);
            outputStream.close();
        } catch (Exception e) {
            log.error("导出失败", e);
            throw new RuntimeException(e);
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (Exception e) {
                    log.error("导出失败", e);
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
